from datetime import datetime
from typing import TypeVar, Generic, Optional

from pydantic import BaseModel, Field

T = TypeVar('T')


class TimestampMixin:
    created_at: Optional[datetime] = Field(default=None, description="创建时间")
    updated_at: Optional[datetime] = Field(default=None, description="更新时间")


class AccessToken(BaseModel):
    access_token: str
    token_type: str = "bearer"


class NewTokenMixin:
    new_token: Optional[AccessToken] = None


class Result(BaseModel, Generic[T], NewTokenMixin):
    ok: bool = True
    code: int = 200
    message: Optional[str] = "OK"
    data: Optional[T]

    def __init__(self, data: Optional[T] = None, **kwargs):
        super().__init__(data=data, **kwargs)
